package com.acce.gxsc.model.http;

import com.acce.gxsc.model.http.gson.GsonConverterFactory;
import com.acce.gxsc.model.http.rxjava.RxJavaCallAdapterFactory;
import com.acce.sdk.BaseApp;
import com.acce.sdk.User;
import com.acce.sdk.utils.AppUtils;
import com.acce.sdk.utils.CommonUtils;
import com.acce.sdk.utils.LogUtils;
import com.acce.sdk.utils.StringUtils;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
/**
 * Created by huxley on 2017/3/25.
 */

public class HttpClient {

    private static AcceApi sAcceApi;

    public static AcceApi getAcceApi() {
        if (sAcceApi == null) {
            synchronized (AcceApi.class) {
                if (sAcceApi == null) {
                    sAcceApi = new Retrofit.Builder()
                            .client(getOkHttpClient())
                            .baseUrl(AcceApi.BASE_URL)
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                            .build()
                            .create(AcceApi.class);
                }
            }
        }
        return sAcceApi;
    }

    public static OkHttpClient getOkHttpClient() {
        Interceptor addQueryParameterInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                String timestamp = CommonUtils.getEditTime();
                String nonce = CommonUtils.getNonce();
                LogUtils.logout(User.getInstance().getToken());
                LogUtils.logout(timestamp);
                LogUtils.logout(nonce);
                String sign = CommonUtils.getSign(User.getInstance().getToken(), timestamp, nonce);
                String deviceName = StringUtils.isEmpty(CommonUtils.getDeviceName()) ? "" : CommonUtils.getDeviceName();
                String ipAddress = StringUtils.isEmpty(CommonUtils.getIpAddress()) ? "" : CommonUtils.getIpAddress();
                String macAddress = StringUtils.isEmpty(CommonUtils.getMacAddress(BaseApp.context)) ? "" : CommonUtils.getMacAddress(BaseApp.context);
                String realName = StringUtils.isEmpty(User.getInstance().getRealName()) ? "" : User.getInstance().getRealName();
                String companyId = StringUtils.isEmpty(User.getInstance().getCompanyId()) ? "" : User.getInstance().getCompanyId();
                String companyName = StringUtils.isEmpty(User.getInstance().getCompanyName()) ? "" : User.getInstance().getCompanyName();
                String departmentId = StringUtils.isEmpty(User.getInstance().getDepartmentId()) ? "" : User.getInstance().getDepartmentId();
                String departmentName = StringUtils.isEmpty(User.getInstance().getDepartmentName()) ? "" : User.getInstance().getDepartmentName();
                String editUserId = StringUtils.isEmpty(User.getInstance().getAtUserId()) ? "" : User.getInstance().getAtUserId();
                String serialNumber = StringUtils.isEmpty(CommonUtils.getSerialNumber()) ? "" : CommonUtils.getSerialNumber();
                String appName = AppUtils.getAppName(BaseApp.context);
                appName = StringUtils.isEmpty(appName) ? "" : appName;
                Request originalRequest = chain.request();
                Request request;
                HttpUrl modifiedUrl = originalRequest.url().newBuilder()
                        .addQueryParameter("sign", sign)
                        .addQueryParameter("nonce", nonce)
                        .addQueryParameter("timestamp", timestamp)
                        .addQueryParameter("device", deviceName)
                        .addQueryParameter("ipaddr", ipAddress)
                        .addQueryParameter("mac", macAddress)
                        .addQueryParameter("realName", realName)
                        .addQueryParameter("companyId", companyId)
                        .addQueryParameter("owerName", companyName)
                        .addQueryParameter("departmentId", departmentId)
                        .addQueryParameter("departmentName", departmentName)
                        .addQueryParameter("editUserId", editUserId)
                        .addQueryParameter("editTime", timestamp)
                        .addQueryParameter("sn", serialNumber)
                        .addQueryParameter("app", appName)
                        .build();
                request = originalRequest.newBuilder().url(modifiedUrl).build();
                return chain.proceed(request);
            }
        };
        return new OkHttpClient.Builder()
                .addInterceptor(addQueryParameterInterceptor)
                .addInterceptor(new OkHttpLoggingInterceptor())
                .build();
    }
}
